import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'

export const countAsync = createAsyncThunk('yyy', () => {
  // 写泛型,是为了明确这个promise成功之后的value值是数字类型
  return new Promise<number>((resolve, reject) => {
    setTimeout(() => {
      resolve(5)
    }, 1000)
  })
})
const slice = createSlice({
  name: 'count',
  initialState: { count: 0, msg: 'xxx' },
  reducers: {
    add(state, action) {
      state.count = state.count + action.payload
    },
  },
  extraReducers(builder) {
    builder.addCase(countAsync.fulfilled, (state, action) => {
      state.count = state.count + action.payload
    })
  },
})
export default slice
